package org.tio.sitexxx.web.server.controller.minip;

import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.slf4j.Logger;
import org.tio.sitexxx.web.server.utils.ObjectToMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tanlin
 */
public class PayParameters {
    public static final PayParameters payParameters=new PayParameters();

    /**
     * 支付参数生成
     * @param openid  用户唯一标识
     * @param pay_amount  需支付金额
     * @param order_id   订单id
     * @param payment_id   payment  表id
     * @param log   日志类
     * @param payMsg  支付信息
     * @param discount_amount  折扣金额
     * @return
     */
    public Map<String, Object> Parameters(String openid, int pay_amount, String order_id, int payment_id, Logger log,String payMsg,int discount_amount){
        Map<String, Object> resultMap = new HashMap<>();
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        //支付信息
        orderRequest.setBody(payMsg);
        //用户openid
        orderRequest.setOpenid(openid);
        //支付金额
        orderRequest.setTotalFee(pay_amount);   //注意：传入的金额参数单位为分
        //outTradeNo  订单号
        orderRequest.setOutTradeNo(order_id);
        //用户IP地址
        orderRequest.setSpbillCreateIp("61.187.92.238");

        Map<String, Object> payparams = new HashMap<>();
        try{
            if (pay_amount > 0){
                Object obj  = wxpay.getInstance().createOrder(orderRequest);
                ObjectToMap.beanToMap(payparams,obj);
                payparams.put("package",payparams.get("packageValue"));
            }
            resultMap.put("ok",0);
            resultMap.put("discount_amount", discount_amount);
            resultMap.put("payparams", payparams);
            resultMap.put("order_id",order_id);//订单号
            resultMap.put("pay_amount", pay_amount);//支付金额
            resultMap.put("payment_id", payment_id);
        }catch (WxPayException e){
            log.error(e.toString(),e);
        } catch(Exception e){
            log.error(e.toString(),e);
            System.out.println(e.toString());
        }
        return resultMap;
    }
}
